package tuling.netty.splitpacket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import tuling.netty.splitpacket.domain.RgsMessageProtocol;

/**
 * @author RenGaoshuai
 * @date 2023/11/30 9:49
 * @description
 */
public class SplitPacketServerHandler extends SimpleChannelInboundHandler<RgsMessageProtocol> {

    private int count;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, RgsMessageProtocol msg) throws Exception {
        System.out.println("====服务端接收到消息如下====");
//        System.out.println("长度=" + msg.getLen());
//        System.out.println("内容=" + new String(msg.getContent(), CharsetUtil.UTF_8));
//        System.out.println("服务端接收到消息包数量=" + (++this.count));

        System.out.println("内容=" + new String(msg.getContent(), CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //关闭通道
        ctx.close();
    }
}
